package com.ruoyi.work.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.work.domain.WorkCustomer;
import com.ruoyi.work.domain.WorkPaper;
import com.ruoyi.work.mapper.WorkCustomerMapper;
import com.ruoyi.work.mapper.WorkPaperMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.work.mapper.WorkInvoiceMapper;
import com.ruoyi.work.domain.WorkInvoice;
import com.ruoyi.work.service.IWorkInvoiceService;
import com.ruoyi.common.core.text.Convert;

/**
 * 申请开票Service业务层处理
 *
 * @author ruoyi
 * @date 2021-12-27
 */
@Service
public class WorkInvoiceServiceImpl implements IWorkInvoiceService {
    @Autowired
    private WorkInvoiceMapper workInvoiceMapper;
    @Autowired
    private WorkPaperMapper workPaperMapper;
    @Autowired
    private WorkCustomerMapper workCustomerMapper;
    /**
     * 查询申请开票
     *
     * @param invoiceId 申请开票主键
     * @return 申请开票
     */
    @Override
    public WorkInvoice selectWorkInvoiceByInvoiceId(Long invoiceId) {
        return workInvoiceMapper.selectWorkInvoiceByInvoiceId(invoiceId);
    }
    /**
     * 查询申请开票
     *
     * @param invoiceId 申请开票主键
     * @return 申请开票
     */
    @Override
    public List<WorkInvoice> selectWorkInvoiceByInvoiceIds(String[] invoiceIds) {
        return workInvoiceMapper.selectWorkInvoiceByInvoiceIds(invoiceIds);
    }
    /**
     * 查询申请开票列表
     *
     * @param workInvoice 申请开票
     * @return 申请开票
     */
    @Override
    public List<WorkInvoice> selectWorkInvoiceList(WorkInvoice workInvoice) {
        return workInvoiceMapper.selectWorkInvoiceList(workInvoice);
    }

    /**
     * 新增申请开票
     *
     * @param workInvoice 申请开票
     * @return 结果
     */
    @Override
    public int insertWorkInvoice(WorkInvoice workInvoice) {
        WorkCustomer customer = workCustomerMapper.selectWorkCustomerByCustomerId(workInvoice.getCustomerId());
        workInvoice.setCustomerName(customer.getCustomerName());
        workInvoice.setCompanyName(customer.getCompanyName());
        //todo 待优化
        String[] ids = workInvoice.getParams().values().toString().replace("[", "").replace("]", "").split(",");
        List<WorkPaper> paperList = workPaperMapper.selectWorkPaperByPaperIds(ids);
        BigDecimal paperPrice = paperList.stream().map(WorkPaper::getActualPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
        workInvoice.setCreateTime(DateUtils.getNowDate());
        workInvoice.setPaperPrice(paperPrice);
        int result = workInvoiceMapper.insertWorkInvoice(workInvoice);


        paperList.forEach(
                item -> {
                    item.setInvoiceId(workInvoice.getInvoiceId());

                    workPaperMapper.updateWorkPaper(item);
                }
        );
        return result;

    }

    /**
     * 修改申请开票
     *
     * @param workInvoice 申请开票
     * @return 结果
     */
    @Override
    public int updateWorkInvoice(WorkInvoice workInvoice) {
        WorkCustomer customer = workCustomerMapper.selectWorkCustomerByCustomerId(workInvoice.getCustomerId());
        workInvoice.setCustomerName(customer.getCustomerName());
        workInvoice.setCompanyName(customer.getCompanyName());
        workPaperMapper.updateWorkPaperInvoice(workInvoice.getInvoiceId());
        //todo 待优化
        String[] ids = workInvoice.getParams().values().toString().replace("[", "").replace("]", "").split(",");
        List<WorkPaper> paperList = workPaperMapper.selectWorkPaperByPaperIds(ids);
        BigDecimal paperPrice = paperList.stream().map(WorkPaper::getPaperPrice).reduce(BigDecimal.ZERO, BigDecimal::add);

        workInvoice.setPaperPrice(paperPrice);
        workInvoice.setUpdateTime(DateUtils.getNowDate());


        paperList.forEach(
                item -> {
                    item.setInvoiceId(workInvoice.getInvoiceId());

                    workPaperMapper.updateWorkPaper(item);
                }
        );

        return workInvoiceMapper.updateWorkInvoice(workInvoice);
    }

    /**
     * 批量删除申请开票
     *
     * @param invoiceIds 需要删除的申请开票主键
     * @return 结果
     */
    @Override
    public int deleteWorkInvoiceByInvoiceIds(String invoiceIds) {
        return workInvoiceMapper.deleteWorkInvoiceByInvoiceIds(Convert.toStrArray(invoiceIds));
    }

    /**
     * 删除申请开票信息
     *
     * @param invoiceId 申请开票主键
     * @return 结果
     */
    @Override
    public int deleteWorkInvoiceByInvoiceId(Long invoiceId) {
        return workInvoiceMapper.deleteWorkInvoiceByInvoiceId(invoiceId);
    }

    @Override
    public List<WorkInvoice> selectInvoiceByCancel(Long incomingId){
        return workInvoiceMapper.selectInvoiceByCancel(incomingId);
    }

    @Override
    public List<WorkInvoice> selectInvoicePaperByCancel(WorkInvoice workInvoice){
        return workInvoiceMapper.selectInvoicePaperByCancel(workInvoice);
    }
}
